package com.junzhiit.nengji.cms.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.cms.service.ShopDeliveryService;
import com.junzhiit.nengji.cms.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.ShopDeliveryAddVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.ShopDeliveryDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.Shop;
import com.junzhiit.nengji.model.ShopDelivery;

@Service
public class ShopDeliveryServiceImpl extends BaseServiceImpl implements ShopDeliveryService {
	
	@Autowired
	private ShopDeliveryDAO shopDeliveryDAO;
	
	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;
	
	@Autowired
	private ShopDAO shopDAO;
	
	@Override
	public ResultVO insert(ShopDeliveryAddVO vo) {
		ResultVO result = new ResultVO();
		
		ShopDelivery shopDelivery = new ShopDelivery();
		
		BeanUtils.copyProperties(vo, shopDelivery);
		
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO.getHash(DIYKey.getSessionHashKey(vo.getSessionId()),
				SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}

		shopDelivery.setShopId(shop.getId());
		result.setData(shopDeliveryDAO.insert(shopDelivery));
		return result;
	}

	@Override
	public ResultVO update(ShopDelivery shopDelivery,String sessionId) {
		ResultVO result = new ResultVO();
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO.getHash(DIYKey.getSessionHashKey(sessionId),
				SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}

		result.setData(shopDeliveryDAO.update(shopDelivery));
		return result;
	}

	@Override
	public ResultVO findAll() {
		ResultVO result = new ResultVO();
		List<ShopDelivery> list = shopDeliveryDAO.findAll();
		result.setData(list);
		return result;
	}

	@Override
	public ResultVO findAllByShopId(String sessionId) {
		ResultVO result = new ResultVO();
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO.getHash(DIYKey.getSessionHashKey(sessionId),
				SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		List<ShopDelivery> list = shopDeliveryDAO.findAllByShopId(shop.getId());
		result.setData(list);
		return result;
	}

	@Override
	public ResultVO getByShopId(String shopDeliveryId) {
		ResultVO result = new ResultVO();
		result.setData(shopDeliveryDAO.query(shopDeliveryId));
		return result;
	}

	@Override
	public ResultVO delete(String shopDeliveryId) {
		ResultVO result = new ResultVO();
		result.setData(shopDeliveryDAO.delete(shopDeliveryId));
		return result;
	}

	@Override
	public ResultVO adapt(String sessionId, Integer type, Integer vehicle) {
		ResultVO result = new ResultVO();
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO.getHash(DIYKey.getSessionHashKey(sessionId),
				SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		result.setData(shopDeliveryDAO.adapt(shop.getId(), type, vehicle));
		return result;
	}
}
